package com.maoning.gulimallcart.vo;

import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 购物车vo
 * @Author : 猫宁
 * @Date: 2022/4/5
 */
@Data
public class Cart {
    private Long id;
    private List<CartItem> cartItems;
    private Integer countNum;//商品数量
    private Integer countType;//商品类型总数
    private BigDecimal totalAmount = new BigDecimal("0.00");;//减免前总价
    private BigDecimal reduce = new BigDecimal("0.00");//减免价格

    public Integer getCountNum() {
        AtomicInteger count = new AtomicInteger(0);
        if(!CollectionUtils.isEmpty(cartItems)){
            this.cartItems.forEach(item -> {
                count.addAndGet(item.getCout());
            });
        }
        return count.get();
    }

    public Integer getCountType() {
        AtomicInteger count = new AtomicInteger(0);
        if(!CollectionUtils.isEmpty(cartItems)){
            this.cartItems.forEach(item -> {
                count.incrementAndGet();
            });
        }
        return count.get();
    }

    public BigDecimal getTotalAmount() {

        if(!CollectionUtils.isEmpty(cartItems)){
            this.cartItems.forEach(item -> {
                this.totalAmount.add(item.getTotalPrice());
            });
        }
        return this.totalAmount;
    }
}
